package com.hotspot.chartview.view;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.hotspot.chartview.R;

/**
 * 数字显示在一个圆形进度条内
 */
public class ProgressText extends View implements AnimatorUpdateListener {

    private int mProgressColor; // 进度条颜色
    private int mStrokeColor; // 进度条边框颜色
    private int mTextColor; // 文本颜色
    private int mTextSize; // 文本大小
    private int mRadius; // 进度条半径

    private Paint mPaintProgress; // 画进度条用的画笔
    private Paint mPaintStroke; // 画边框用的画笔
    private Paint mPaintText; // 画文字用的画笔

    private ObjectAnimator mAnimate; // 动画
    private float mPhase = 1f;

    private int mProgress; // 进度

    public ProgressText(Context context) {
        this(context, null, 0);
    }

    public ProgressText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (attrs != null) {
            Resources res = getResources();
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressText);
            mProgressColor = typedArray.getColor(R.styleable.ProgressText_progress_color,
                    res.getColor(R.color.progress_color_default));
            mStrokeColor = typedArray.getColor(R.styleable.ProgressText_stroke_color,
                    res.getColor(R.color.progress_stroke_color_default));
            mTextColor = typedArray.getColor(R.styleable.ProgressText_text_color,
                    res.getColor(R.color.progress_text_color_default));
            mTextSize = typedArray.getDimensionPixelOffset(R.styleable.ProgressText_text_size, 35);
            mRadius = typedArray.getDimensionPixelSize(R.styleable.ProgressText_radius, 45);
            typedArray.recycle();
        }
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        /**
         * 进度条画笔
         */
        mPaintProgress = new Paint();
        mPaintProgress.setColor(mProgressColor);
        mPaintProgress.setAntiAlias(true);
        mPaintProgress.setStrokeWidth(12);
        mPaintProgress.setStyle(Paint.Style.STROKE);

        /**
         * 边框画笔
         */
        mPaintStroke = new Paint();
        mPaintStroke.setColor(mStrokeColor);
        mPaintStroke.setAntiAlias(true);
        mPaintStroke.setStrokeWidth(1);
        mPaintStroke.setStyle(Paint.Style.STROKE);

        /**
         * 文字画笔
         */
        mPaintText = new Paint();
        mPaintText.setColor(mTextColor);
        mPaintText.setTextSize(mTextSize);
        mPaintText.setAntiAlias(true);
        mPaintText.setStrokeWidth(1);
        mPaintText.setTextAlign(Align.CENTER);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawStroke(canvas); // 画进度条的内外边框
        drawProgress(canvas); // 画进度条
        drawText(canvas); // 画文本
    }

    /**
     * 画进度条的内外边框
     * 
     * @param canvas
     *            画布
     */
    private void drawStroke(Canvas canvas) {
        canvas.save();
        float innerRadius = mRadius - mPaintProgress.getStrokeWidth();
        canvas.translate(mRadius, mRadius);
        canvas.drawCircle(0, 0, mRadius - mPaintStroke.getStrokeWidth(), mPaintStroke); // 外边框
        canvas.drawCircle(0, 0, innerRadius, mPaintStroke); // 内边框
        canvas.restore();
    }

    /**
     * 画进度条
     * 
     * @param canvas
     *            画布
     */
    private void drawProgress(Canvas canvas) {
        canvas.save();
        float strokeWidth = mPaintStroke.getStrokeWidth(); // 边框的宽度
        float progressStroke = mPaintProgress.getStrokeWidth(); // 进度条宽度
        canvas.translate(progressStroke / 2 + strokeWidth, progressStroke / 2 + strokeWidth); // 坐标变换，弧形的半径只包括画笔宽度的一半
        float progressRadius = mRadius - progressStroke / 2;
        RectF rectF = new RectF(0, 0, progressRadius * 2, progressRadius * 2);
        float sweepDegree = -mProgress / 100.0f * 360;
        canvas.drawArc(rectF, -90, mPhase * (int) sweepDegree, false, mPaintProgress);
        canvas.restore();
    }

    /**
     * 画文字
     * 
     * @param canvas
     *            画布
     */
    private void drawText(Canvas canvas) {
        Rect rect = new Rect(0, 0, mRadius * 2, mRadius * 2);
        FontMetricsInt fontMetrics = mPaintText.getFontMetricsInt();
        int baseLine = rect.top + (rect.bottom - rect.top - fontMetrics.bottom + fontMetrics.top)
                / 2 - fontMetrics.top;
        canvas.drawText("" + (int) (mPhase * mProgress), rect.centerX(), baseLine, mPaintText);
    }

    /**
     * phase的setter方法，与ObjectAnimator配合使用
     * 
     * @param phase
     *            动画执行的进度
     */
    public void setPhase(float phase) {
        mPhase = phase;
    }

    /**
     * 设置文本信息
     * 
     * @param text
     */
    public void setProgress(int progress) {
        mProgress = progress;
    }

    /**
     * 动画
     * 
     * @param durationMillis
     *            执行时间
     */
    public void startAnimation(int durationMillis) {
        mAnimate = ObjectAnimator.ofFloat(this, "phase", 0f, 1f);
        mAnimate.setDuration(durationMillis);
        mAnimate.addUpdateListener(this);
        mAnimate.start();
    }

    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        invalidate();
    }

}
